package com.gr

import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Person)
class PersonTests {

	def person
	def company = new Company(title: 'duinc', email: 'duinc@gmail.com', startDate: new Date("2001/1/18"))
	def division = new Division(title: 'DivisionA', email: 'divisiona@gmail.com', startDate: new Date("2011/3/21"), companyTo: company)
	def project = new Project(title:"Project A1", email:"personA1@duorg.com", startDate:new Date("8/18/2012"), divisionTo:division)

	@Before
	void setUp() {
		// Make sure we can invoke validate() on our User domain object.
		mockForConstraintsTests(Person)
		// Set up default person, so we can easily test single properties.
		person = new Person(name: "John Gardner", email: "john.gardner@duorg.com", birthDate:new Date("1960/5/15"), hiredDate:new Date("2009/1/5"), projectTo:project)
	}

	/**
	 * 	name(unique...) no rule
	 */
	void testConstraintsNameUnique() {

		def test = new Person(name: "John Gardner", email: "john.gardner2@duorg.com", birthDate:new Date("1960/5/15"), hiredDate:new Date("2009/1/5"), projectTo:project)
		mockForConstraintsTests(Person, [test])

		assertTrue person.validate()
	}

	/**
	 * name(blank: false)
	 */	
	void testConstraintsNameBlank() {
		person.name = ''
		assertFalse person.validate()
		assertEquals 'blank', person.errors['name']

		person.name = 'notempty'
		assertTrue person.validate()
	}

	/**
	 * name(size: 2..50)
	 */
	void testConstraintsNameSize() {
		person.name = 'v'
		assertFalse person.validate()
		assertEquals 'size', person.errors['name']

		person.name = '123456789012345678901234567890123456789012345678901'
		assertFalse person.validate()
		assertEquals 'size', person.errors['name']

		person.name = 'normalsize'
		assertTrue person.validate()
	}

	/**
	 * email(email: true)
	 */
	void testConstraintsEmailEmail() {
		person.email = 'somesting'
		assertFalse person.validate()
		assertEquals 'email', person.errors['email']

		person.email = 'valid@email.ru'
		assertTrue person.validate()
	}

	/**
	 * email(nullable: true)
	 */
	void testConstraintsEmailNullable() {
		person.email = ''
		assertTrue person.validate()
		assertNull person.errors['email']
	}

	/**
	 * birthDate(nullable: true)
	 */
	void testConstraintsBirthDateNullable() {
		person.birthDate = null
		assertTrue person.validate()
		assertNull person.errors['birthDate']
	}

	/**
	 * hiredDate(nullable: true)
	 */
	void testConstraintsHiredDateNullable() {
		person.hiredDate = null
		assertTrue person.validate()
		assertNull person.errors['hiredDate']
	}

	/**
	 * projectTo(blank: false)
	 */
	void testConstraintsCompanyToBlank() {
		person.projectTo = null
		assertFalse person.validate()
		assertEquals 'nullable', person.errors['projectTo']

		person.projectTo = project
		assertTrue person.validate()
	}
}
